package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Dao.QuestionsAnswersRepository;
import com.realtyManage.springboot.Model.Do.QuestionsAnswers;
import com.realtyManage.springboot.Service.IQuestionsAnswersService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING;

/**
 * SpecialOfferHouseServiceImpl
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/5/15
 **/
@Service
@Transactional
public class QuestionsAnswersServiceImpl implements IQuestionsAnswersService {
    @Autowired
    private QuestionsAnswersRepository questionsAnswersRepository;

    @Override
    public QuestionsAnswers save(QuestionsAnswers questionsAnswers) {
        return questionsAnswersRepository.save(questionsAnswers);
    }

    @Override
    public List<QuestionsAnswers> findQuestionsAnswers(String center) {
        if (center != null){
            return questionsAnswersRepository.findQuestionsAnswersE(center);
        }else{
            return questionsAnswersRepository.findQuestionsAnswers();
        }
    }

    @Override
    public List<QuestionsAnswers> findQuestionsList() {
        return questionsAnswersRepository.findQuestionsList();
    }

    @Override
    public int setAnswers(QuestionsAnswers questionsAnswers) {
        return questionsAnswersRepository.setAnswers(questionsAnswers);
    }

    @Override
    public List<QuestionsAnswers> findQuestionsById(Integer id) {
        return questionsAnswersRepository.findQuestionsById(id);
    }

    @Override
    public int importQuestionsAnswersList(MultipartFile file, HttpServletRequest request) {
        List<QuestionsAnswers> list = new ArrayList<QuestionsAnswers>();
        //1.得到上传的表
        Workbook workbook2 = null;
        try {
            workbook2 = WorkbookFactory.create(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        //2、获取sheet1工作表
        Sheet sheet2 = workbook2.getSheet("Sheet1");
        //获取表的总行数
        int num = sheet2.getLastRowNum();
        //总列数
        int col = sheet2.getRow(0).getLastCellNum();
        //遍历excel每一行
        for (int j = 1; j < num + 1; j++) {
            Row row1 = sheet2.getRow(j);
            //获取表中第i行，第1列的单元格
            Cell cell3 = row1.getCell(0);  //问
            if (cell3 != null){
                cell3.setCellType(CELL_TYPE_STRING);
            }
            Cell cell4 = row1.getCell(1);  //答
            if(cell4 != null){
                cell4.setCellType(CELL_TYPE_STRING);
            }
            //这里new 一个对象，用来装填从页面上传的Excel数据，字段根据上传的excel决定
            QuestionsAnswers excel= new QuestionsAnswers();
            if (cell3 != null){
                excel.setAnswers(cell3.toString());
            }
            if (cell4 != null){
                excel.setQuestions(cell4.toString());
            }
            //默认操作
            excel.setTime(new Date());
            excel.setIsShow(0);
            list.add(excel);
        }
        questionsAnswersRepository.saveAll(list);
        return 0;
    }

    @Override
    public int deleteQuestions(Integer id) {
        return questionsAnswersRepository.deleteQuestions(id);
    }

}
